package com.lyh.web.controller.app;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyh.common.core.domain.entity.SysUser;
import com.lyh.common.utils.SecurityUtils;
import com.lyh.system.domain.vo.GoodsVo;
import com.lyh.system.mapper.ReceiveAddressMapper;
import com.lyh.system.mapper.RechargeRecordMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lyh.common.annotation.Log;
import com.lyh.common.core.controller.BaseController;
import com.lyh.common.core.domain.AjaxResult;
import com.lyh.common.enums.BusinessType;
import com.lyh.system.domain.ReceiveAddress;
import com.lyh.system.service.IReceiveAddressService;
import com.lyh.common.utils.poi.ExcelUtil;
import com.lyh.common.core.page.TableDataInfo;

/**
 * 收货地址Controller
 *
 * @author ruoyi
 * @date 2023-05-11
 */
@RestController
@RequestMapping("/app/address")
public class ReceiveAddressController extends BaseController {
    @Autowired
    private IReceiveAddressService receiveAddressService;
    @Autowired
    private ReceiveAddressMapper receiveAddressMapper;

    /*    *//**
     * 查询收货地址列表
     *//*
//    @PreAuthorize("@ss.hasPermi('system:address:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReceiveAddress receiveAddress)
    {
//        startPage();
        List<ReceiveAddress> list = receiveAddressService.selectReceiveAddressList(receiveAddress);
        return getDataTable(list);
    }*/

    /**
     * 导出收货地址列表
     */
    @PreAuthorize("@ss.hasPermi('system:address:export')")
    @Log(title = "收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReceiveAddress receiveAddress) {
        List<ReceiveAddress> list = receiveAddressService.selectReceiveAddressList(receiveAddress);
        ExcelUtil<ReceiveAddress> util = new ExcelUtil<ReceiveAddress>(ReceiveAddress.class);
        util.exportExcel(response, list, "收货地址数据");
    }

    /**
     * 获取收货地址详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(receiveAddressService.selectReceiveAddressById(id));
    }

    /**
     * 查询收货地址列表
     */
    @GetMapping("/list")
    public AjaxResult list() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<ReceiveAddress> receiveAddresses = receiveAddressMapper.selectList(new LambdaQueryWrapper<ReceiveAddress>()
                .eq(ReceiveAddress::getUserId, user.getUserId()));
        return AjaxResult.success(receiveAddresses);
    }

    /**
     * 修改收货地址
     */
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ReceiveAddress receiveAddress) {
        if (receiveAddress.getIsDefault() == 1) {
            List<ReceiveAddress> receiveAddresses = receiveAddressMapper.selectList(new LambdaQueryWrapper<ReceiveAddress>()
                    .eq(ReceiveAddress::getIsDefault, 1));
            for (ReceiveAddress address : receiveAddresses) {
                address.setIsDefault(0);
                receiveAddressMapper.updateById(address);
            }
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        receiveAddress.setUserId(user.getUserId());
        if (receiveAddress.getId() != null) {
            receiveAddressMapper.updateById(receiveAddress);
        }else {
            receiveAddressMapper.insert(receiveAddress);
        }
        return AjaxResult.success();
    }

/*    *//**
     * 新增收货地址
     *//*
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ReceiveAddress receiveAddress) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        receiveAddress.setUserId(user.getUserId());
        if (receiveAddress.getIsDefault() == 1) {
            ReceiveAddress address = receiveAddressMapper.selectOne(new LambdaQueryWrapper<ReceiveAddress>()
                    .eq(ReceiveAddress::getIsDefault, 1));
            if (address != null) {
                throw new RuntimeException("已经存在默认地址了");
            }
        }
        receiveAddressMapper.insert(receiveAddress);
        return AjaxResult.success();
    }*/

    /**
     * 删除收货地址
     */
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    public AjaxResult remove(String ids) {
        if (ids != null && !"".equals(ids)) {
            String[] split = ids.split(",");
            List<String> strings = Arrays.asList(split);
            receiveAddressMapper.deleteBatchIds(strings);
            return AjaxResult.success();
        } else {
            return AjaxResult.error("参数错误！");
        }


    }

}
